﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace monitoring_system.Converter
{
    internal class CircleProgressBarConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            double PI2 = Math.PI * 2;
            double centerX = (double)values[0] / 2;
            double centerY = (double)values[1] / 2;
            double angle = (double)values[2] / 100 * PI2;
            double radius = 40;
            double start_x_offset = Math.Sin(0);//起始弧度x偏移
            double start_y_offset = Math.Cos(0);//起始弧度y偏移
            double end_x_offset = Math.Sin(angle);//终止弧度x偏移
            double end_y_offset = Math.Cos(angle);//终止弧度y偏移
            Point start_point = new Point(centerX + radius * start_x_offset, centerY - radius * start_y_offset);
            Point end_point = new Point(centerX + radius * end_x_offset, centerY - radius * end_y_offset);
            var converter = TypeDescriptor.GetConverter(typeof(Geometry));
            string data = string.Empty;
            if (angle == 0)
            {
                data = string.Empty;
            }
            else if (angle > 0 && angle < Math.PI)
            {
                data = $"M{start_point.X},{start_point.Y} A{radius},{radius},0,0,1 {end_point.X},{end_point.Y}";
            }
            else if (angle >= Math.PI && angle < PI2)
            {
                data = $"M{start_point.X},{start_point.Y} A{radius},{radius},0,1,1 {end_point.X},{end_point.Y}";
            }
            else
            {
                data = $"M{start_point.X},{start_point.Y} A{radius},{radius},0,1,1 {end_point.X - 0.001},{end_point.Y} Z";
            }



            return (Geometry)converter.ConvertFrom(data);
        
    }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
